﻿using Common.Models;
using Common.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Wallpaper.Models.DTOs.Requests.Carousel;
using Wallpaper.Models.DTOs.Responses.Carousel;
using Wallpaper.Models.Entities;
using Wallpaper.Services.IService;

namespace WallpaperApi.Controllers
{
    [Route("api/carousels")]
    [ApiController]
    [Authorize]
    public class CarouselController : BaseController
    {
        readonly ILogger<CarouselController> _logger;
        readonly ICarouselService _carouselService;

        public CarouselController(ILogger<CarouselController> logger, ICarouselService carouselService)
        {
            _logger = logger;
            _carouselService = carouselService;
        }


        /// <summary>
        /// 创建封面
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize("system:carousels:add")]
        public async Task<ApiResponse<Object>> Create([FromBody] CarouselRequest carouselRequest)
        {

            bool result = await _carouselService.CreateAsync(carouselRequest);
            _logger.LogInformation($"{Account}创建了封面");
            return ApiResponse<Object>.Success();
        }

        /// <summary>
        /// 更新封面
        /// </summary>
        /// <param name="id"></param>
        /// <param name="carouselRequest"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [Authorize("system:carousels:edit")]
        public async Task<ApiResponse<Object>> Update(string id,
            [FromBody] CarouselRequest carouselRequest)
        {
            bool result = await _carouselService.UpdateAsync(id, carouselRequest);
            return ApiResponse<Object>.Success();
        }



        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize("system:carousels:query")]
        public async Task<ApiResponse<PagedResult<CarouselPageResponse>>> Page([FromQuery] CarouselQueryRequest request)
        {
            PagedResult<CarouselPageResponse> result = await _carouselService.PageAsync(request);
            return ApiResponse<PagedResult<CarouselPageResponse>>.Success(result);
        }

        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [Authorize("system:carousels:delete")]
        public async Task<ApiResponse<Object>> Delete(string id)
        {
            bool result =await _carouselService.DeleteAsync(id);
            return ApiResponse<Object>.Success(result);
        }


        /// <summary>
        /// 小程序端获取轮播图
        /// </summary>
        /// <returns></returns>
        [HttpGet("banners")]
        [AllowAnonymous]
        public async Task<ApiResponse<List<BannerResponse>>> GetBanners()
        {
            List<BannerResponse> bannerResponses= await _carouselService.GetBannersAsync();
            return ApiResponse<List<BannerResponse>>.Success(bannerResponses);
        }
    }
}
